/*
 * app_button.h
 *
 *  Created on: 2023年7月11日
 *      Author: zlw
 */

#ifndef DS_APP_INS_APP_BUTTON_H_
#define DS_APP_INS_APP_BUTTON_H_

#include "stddef.h"
#include "stdint.h"
#include "stdbool.h"
#include "string.h"
#include "softwareMultiTimer.h"

#define DEBOUNCE_CNT    (3)
#define BUTTON_TIMER_SOURCE     (EN_LP_HARD_TIMER_EM3_SYS_RTC_1)

#define DEBOUNCE_INTERVAL   (328)   //10ms
#define POLLING_INTERVAL    (6554)  //200ms

#define SHORT_PRESS_MAX      (5)
#define SHORT_PRESS_TICKS    (1) // time = ticks * POLLING_INTERVAL
#define LONG_PRESS_TICKS     (25) // time = ticks * POLLING_INTERVAL
#define LONG_PRESS_INTERVAL  (LONG_PRESS_TICKS+10)

typedef void (*BtnCallback)(void*);

typedef enum{
  PRESS_DOWN = 0,
  PRESS_UP,
  PRESS_REPEAT,
  SINGLE_CLICK,
  DOUBLE_CLICK,
  LONG_PRESS_START,
  LONG_PRESS_HOLD,
  number_of_event,
  NONE_PRESS
}PressEvent;

typedef struct Button{
  uint16_t ticks;
  uint8_t  repeat;
  uint8_t  event;
  uint8_t  state;
  uint8_t  debounce_cnt : 6;
  uint8_t  active_level : 2;
  uint8_t  button_id;
  uint8_t  (*hal_button_Level)(uint8_t button_id_);
  BtnCallback  cb[number_of_event];
  MultiTimer *buttonTimer;
  MultiTimerCallback_t timerCallback;
}Button;


void button_init(struct Button* handle, MultiTimer *buttonTimer, MultiTimerCallback_t timerCallback,uint8_t(*pin_level)(uint8_t), uint8_t active_level, uint8_t button_id);
void button_attach(struct Button* handle, PressEvent event, BtnCallback cb);
void button_handler(struct Button* handle);

#endif /* DS_APP_INS_APP_BUTTON_H_ */
